<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        class EventBus {
            constructor() {
                this.eventMap = {}
            }
            on(type, fn) {
                if (this.eventMap[type]) {
                    this.eventMap[type].push(fn)
                } else {
                    this.eventMap[type] = [fn]
                }
            }
            emit(type, ...args) {
                if (this.eventMap[type]) {
                    let tasks = this.eventMap[type].slice()
                    tasks.forEach(fn => {
                        fn(...args)
                    })
                }
            }
            off(type, fn) {
                if (this.eventMap[type]) {
                    this.eventMap[type] = this.eventMap[type].filter(item => item !== fn)
                }
            }
        }

        let eventBus = new EventBus()
        let fn1 = function (name, age) {
            console.log(`${name} ${age}`)
        }
        let fn2 = function (name, age) {
            console.log(`hello, ${name} ${age}`)
        }
        eventBus.on('aaa', fn1)
        eventBus.emit('aaa', 'zhangsan', 18)
        eventBus.off('aaa', fn1)

        eventBus.on('aaa', fn2)
        eventBus.emit('aaa', 'zhangsan', 18)
        eventBus.off('aaa', fn2)
    </script>
</body>

</html>